package com.ecgsystem.listadapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.storage.StorageManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.TextView;

import com.ecgsystem.activity.*;
import com.ecgsystem.constants.Constants;
import com.ecgsystem.R;

public class SignalListAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private ArrayList<HashMap<String, String>> filteredData;
	private static LayoutInflater inflater = null;
	private int positions[];
	private int defaultPos;

	public SignalListAdapter(Activity a, ArrayList<HashMap<String, String>> d,
			int defaultPos) {
		activity = a;
		data = d;
		filteredData = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		positions = new int[data.size()];
		for (int i = 0; i < data.size(); i++) {
			positions[i] = i;
		}
		this.defaultPos = defaultPos;
	}

	public int getCount() {
		if (filteredData != null) {
			return filteredData.size();
		} else
			return 0;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public int[] getPositionList() {
		return positions;
	}

	public static class ViewHolder {
		public TextView id_TV;
		public TextView count_TV;
		public TextView start_TV;
		public TextView end_TV;
		public TextView qrs_TV;
		public TextView hr_TV;
		public TextView rs_TV;
		public TextView qr_TV;
		public TextView r_TV;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.list_item_unusualsignal, null);
			holder = new ViewHolder();
			holder.count_TV = (TextView) vi.findViewById(R.id.textView_count);
			holder.id_TV = (TextView) vi.findViewById(R.id.textView_ID);
			holder.start_TV = (TextView) vi
					.findViewById(R.id.textView_starttime);
			holder.end_TV = (TextView) vi.findViewById(R.id.textView_endtime);
			holder.qrs_TV = (TextView) vi.findViewById(R.id.textView_qrs);
			holder.hr_TV = (TextView) vi.findViewById(R.id.textView_hr);
			holder.qr_TV = (TextView) vi.findViewById(R.id.textView_qr);
			holder.rs_TV = (TextView) vi.findViewById(R.id.textView_rs);
			holder.r_TV = (TextView) vi.findViewById(R.id.textView_r);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.count_TV.setText(filteredData.get(position).get("COUNT")
				.toString());
		holder.id_TV.setText(filteredData.get(position)
				.get(Constants.TAG_UNUSUALSIGNAL_ID).toString());
		holder.start_TV.setText(filteredData.get(position)
				.get(Constants.TAG_START_TIME).toString());
		holder.end_TV.setText(filteredData.get(position)
				.get(Constants.TAG_END_TIME).toString());
		holder.qrs_TV.setText(filteredData.get(position).get(Constants.TAG_QRS)
				.toString());
		holder.hr_TV.setText(filteredData.get(position)
				.get(Constants.TAG_HEART_RATE).toString());
		holder.qr_TV.setText(filteredData.get(position).get(Constants.TAG_QR)
				.toString());
		holder.rs_TV.setText(filteredData.get(position).get(Constants.TAG_RS)
				.toString());
		holder.r_TV.setText(filteredData.get(position).get(Constants.TAG_R)
				.toString());

		if (OfflineECG.firstTimeStartup && position == defaultPos) {
			vi.setBackgroundColor(Color.DKGRAY);
		} else {
			vi.setBackgroundColor(Color.TRANSPARENT);
		}

		return vi;
	}

	public Filter getFilter() {
		Filter filter = new Filter() {
			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				FilterResults retval = new FilterResults();
				retval.values = data;
				retval.count = data.size();
				if (constraint != null && constraint.toString().length() > 0) {
					constraint = constraint.toString().toUpperCase();
					ArrayList<HashMap<String, String>> filteredData = new ArrayList<HashMap<String, String>>();
					HashMap<String, String> item;

					int j = 0;
					for (int i = 0; i < data.size(); i++) {
						item = data.get(i);
						if (item.get("ID").toUpperCase().contains(constraint)
								|| item.get(Constants.TAG_HEART_RATE)
										.toUpperCase().contains(constraint)) {
							filteredData.add(item);

							// update position array
							positions[j] = i;
							j++;
						}
					}
					retval.count = filteredData.size();
					retval.values = filteredData;
				} else {
					synchronized (this) {
						retval.count = data.size();
						retval.values = data;
					}
				}
				return retval;
			}

			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				filteredData = (ArrayList<HashMap<String, String>>) results.values;

				notifyDataSetChanged();

			}
		};
		return filter;

	}
}